package org.ru.mse10.cvis.service.search;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.ru.mse10.cvis.dto.cv.CvSearchDTO;
import org.ru.mse10.cvis.entity.cv.CvData;
import org.ru.mse10.cvis.util.CommonUtil;

/**
 * The Class CVSearcher.
 */
public class CVSearcher extends EntitySearcher<CvData, CvSearchDTO> {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ru.mse10.cvis.service.search.EntitySearcher#appendCriteria(javax.
	 * persistence.criteria.CriteriaBuilder,
	 * javax.persistence.criteria.CriteriaQuery,
	 * javax.persistence.criteria.Root, org.ru.mse10.cvis.dto.BaseDTO)
	 */
	@Override
	protected <X> void appendCriteria(CriteriaBuilder qb, CriteriaQuery<X> c,
			Root<CvData> p, CvSearchDTO dto) {
		List<Predicate> criteria = new ArrayList<Predicate>();
		if (!CommonUtil.isEmpty(dto.getTitle())) {
			Path<Object> path = p.join("personalInformation").get("title");
			c.where(qb.equal(path, dto.getTitle()));
		}
		if (!CommonUtil.isEmpty(dto.getCountry())) {
			Path<Object> path = p.join("address").get("country");
			c.where(qb.equal(path, dto.getCountry()));
		}
		if (!CommonUtil.isEmpty(dto.getCity())) {
			Path<Object> path = p.join("address").get("city");
			c.where(qb.equal(path, dto.getCity()));
		}
		if (!CommonUtil.isEmpty(dto.getDateOfBirth())) {
			Path<Object> path = p.join("personalInformation")
					.get("dateOfBirth");
			c.where(qb.equal(path, dto.getDateOfBirth()));
		}
		if (!CommonUtil.isEmpty(dto.getNationality())) {
			Path<Object> path = p.join("personalInformation")
					.get("nationality");
			c.where(qb.equal(path, dto.getNationality()));
		}
		if (!CommonUtil.isEmpty(dto.getRole())) {
			Path<Object> path = p.join("positions").get("role");
			c.where(qb.equal(path, dto.getRole()));
		}
		if (!CommonUtil.isEmpty(dto.getExpirience())) {
			Path<Object> path = p.join("professionalExpiriance").get(
					"description");
			c.where(qb.equal(path, dto.getExpirience()));
		}
		if (!CommonUtil.isEmpty(dto.getSkill())) {
			Path<Object> path = p.join("otherSkills").get("skill");
			c.where(qb.equal(path, dto.getSkill()));
		}
		if (!CommonUtil.isEmpty(dto.getLanguage())) {
			Path<Object> path = p.join("languageSkills").get("language");
			c.where(qb.equal(path, dto.getLanguage()));
		}
		if (!CommonUtil.isEmpty(dto.getInstitution())) {
			Path<Object> path = p.join("educationEntries").get("institution");
			c.where(qb.equal(path, dto.getInstitution()));
		}
		if (!CommonUtil.isEmpty(dto.getDegree())) {
			Path<Object> path = p.join("educationEntries").get("degree");
			c.where(qb.equal(path, dto.getDegree()));
		}
		if (!CommonUtil.isEmpty(dto.getQualification())) {
			Path<Object> path = p.join("keyQualifications")
					.get("qualification");
			c.where(qb.equal(path, dto.getQualification()));
		}
		c.where(criteria.toArray(new Predicate[] {}));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ru.mse10.cvis.service.search.EntitySearcher#getEntityClass()
	 */
	@Override
	public Class<CvData> getEntityClass() {
		return CvData.class;
	}

}
